<?php 
class ModelProductAttribute extends Model {
	public function addAttribute($data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("INSERT INTO " . DB_PREFIX . "attribute SET attribute_group_id = '" . (int)$data['attribute_group_id'] . 
			"', `name` = '" . $name . "'");
		$attribute_id = $this->db->getLastId();
		
		foreach ($data['attribute_description'] as $language_id => $value) {
			$sql = "INSERT INTO " . DB_PREFIX . "attribute_description SET attribute_id = '" . (int)$attribute_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql .= $this->db->escape($value['display_name']) . "'";
			}else{
				$sql .= $name . "'";
			}
			$this->db->query($sql);
		}
		
		if (isset($data['attribute_value'])) {
			foreach ($data['attribute_value'] as $attribute_value) {
				$name_value =  $this->db->escape($attribute_value['name']);
				$sql_value = "INSERT INTO " . DB_PREFIX . "attribute_value SET attribute_id = '" . (int)$attribute_id . 
					"', `name` = '" . $name_value;
				if($attribute_value['image']){
					$sql_value .= "', image = '" . $this->db->escape(html_entity_decode($attribute_value['image'], ENT_QUOTES, 'UTF-8'));
				}
				$sql_value .= "', sort_order = '" . (int)$attribute_value['sort_order'] . "'";
				$this->db->query($sql_value);
				$attribute_value_id = $this->db->getLastId();
				
				foreach ($attribute_value['attribute_value_description'] as $language_id => $attribute_value_description) {
					$sql_value_desc = "INSERT INTO " . DB_PREFIX . "attribute_value_description SET attribute_value_id = '" . (int)$attribute_value_id . 
						"', language_id = '" . (int)$language_id . 
						"', display_name = '";
					if($attribute_value_description['display_name']){
						 $sql_value_desc .=  $this->db->escape($attribute_value_description['display_name']) . "'";
					}else{
						$sql_value_desc .=  $name_value . "'";
					}
					$this->db->query($sql_value_desc);
				}
			}
		}
	}

	public function editAttribute($attribute_id, $data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("UPDATE " . DB_PREFIX . "attribute SET attribute_group_id = '" . (int)$data['attribute_group_id'] . 
			"', `name` = '" . $name . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int)$attribute_id . "'");
		foreach ($data['attribute_description'] as $language_id => $value) {
			$sql = "INSERT INTO " . DB_PREFIX . "attribute_description SET attribute_id = '" . (int)$attribute_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql .= $this->db->escape($value['display_name']) . "'";
			}else{
				$sql .= $name . "'";
			}
			$this->db->query($sql);
		}
		
		if (isset($data['attribute_value'])) {
			foreach ($data['attribute_value'] as $attribute_value) {
				$name_value =  $this->db->escape($attribute_value['name']);
				if ($attribute_value['attribute_value_id']) {
					$attribute_value_id = $attribute_value['attribute_value_id'];
					$sql_value = "UPDATE " . DB_PREFIX . "attribute_value SET attribute_id = '" . (int)$attribute_id . 
						"', `name` = '" . $name_value;
					if($attribute_value['image']){
						$sql_value .= "', image = '" . $this->db->escape(html_entity_decode($attribute_value['image'], ENT_QUOTES, 'UTF-8')) . "'";
					}else{
						$sql_value .= "', image = null";
					}
					$sql_value .= ", sort_order = '" . (int)$attribute_value['sort_order'] . 
						"' WHERE attribute_value_id = '" . (int)$attribute_value_id . "'";
					$this->db->query($sql_value);
				} else {
					$sql_value = "INSERT INTO " . DB_PREFIX . "attribute_value SET attribute_id = '" . (int)$attribute_id . 
						"', `name` = '" . $name_value;
					if($attribute_value['image']){
						$sql_value .= "', image = '" . $this->db->escape(html_entity_decode($attribute_value['image'], ENT_QUOTES, 'UTF-8'));
					}
					$sql_value .= "', sort_order = '" . (int)$attribute_value['sort_order'] . "'";
					$this->db->query($sql_value);
					$attribute_value_id = $this->db->getLastId();
				}
				
				$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_value_description WHERE attribute_value_id = '" . (int)$attribute_value_id ."'");
				foreach ($attribute_value['attribute_value_description'] as $language_id => $attribute_value_description) {
					$sql_value_desc = "INSERT INTO " . DB_PREFIX . "attribute_value_description SET attribute_value_id = '" . (int)$attribute_value_id . 
						"', language_id = '" . (int)$language_id . 
						"', display_name = '";
					if($attribute_value_description['display_name']){
						 $sql_value_desc .=  $this->db->escape($attribute_value_description['display_name']) . "'";
					}else{
						$sql_value_desc .=  $name_value . "'";
					}
					$this->db->query($sql_value_desc);
				}
			}
		}
	}
		
	public function getAttribute($attribute_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute WHERE attribute_id = '" . (int)$attribute_id . "'");
		return $query->row;
	}
	
	public function getAttributeDescriptions($attribute_id) {
		$attribute_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int)$attribute_id . "'");
		foreach ($query->rows as $result) {
			$attribute_data[$result['language_id']] = array(
				'attribute_id' => $result['attribute_id'],
				'display_name' => $result['display_name']
			);
		}
		return $attribute_data;
	}
	
	public function getAttributeValues($attribute_id) {
		$attribute_value_query = $this->db->query("SELECT av.*, avd.display_name FROM " . DB_PREFIX . "attribute_value av LEFT JOIN " . 
			DB_PREFIX . "attribute_value_description avd ON (av.attribute_value_id = avd.attribute_value_id AND avd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "') WHERE av.attribute_id = '" . (int)$attribute_id . "' ORDER BY av.sort_order ASC");
		return $attribute_value_query->rows;
	}
	
	public function getAttributeValueDescriptions($attribute_id) {
		$attribute_value_data = array();
		$attribute_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute_value WHERE attribute_id = '" . (int)$attribute_id . "'");
		foreach ($attribute_value_query->rows as $attribute_value) {
			$attribute_value_description_data = array();
			$attribute_value_description_query = $this->db->query("SELECT * FROM " . 
				DB_PREFIX . "attribute_value_description WHERE attribute_value_id = '" . (int)$attribute_value['attribute_value_id'] . "'");			
			foreach ($attribute_value_description_query->rows as $attribute_value_description) {
				$attribute_value_description_data[$attribute_value_description['language_id']] = array(
					'attribute_value_id' => $attribute_value_description['attribute_value_id'],
					'display_name' => $attribute_value_description['display_name']
				);
			}
			$attribute_value_data[] = array(
				'attribute_value_id'          => $attribute_value['attribute_value_id'],
				'name'                        => $attribute_value['name'],
				'image'                       => $attribute_value['image'],
				'sort_order'                  => $attribute_value['sort_order'],
				'attribute_value_description' => $attribute_value_description_data
			);
		}
		
		return $attribute_value_data;
	}
		
	public function getAttributes($data = array()) {
		$lang_id = (int)$this->config->get('config_language_id');
		$sql = "SELECT a.`attribute_id`, a.`attribute_group_id`, a.`name`, ad.display_name, ag.`name` \"group_name\", agd.display_name \"group_display_name\" FROM " . 
			DB_PREFIX . "attribute a LEFT JOIN " . 
			DB_PREFIX . "attribute_description ad ON (a.`attribute_id` = ad.`attribute_id` AND ad.language_id = '" . $lang_id . "') LEFT JOIN " . 
			DB_PREFIX . "attribute_group ag ON a.`attribute_group_id` = ag.attribute_group_id LEFT JOIN " . 
			DB_PREFIX . "attribute_group_description agd ON (ag.`attribute_group_id` = agd.attribute_group_id AND agd.language_id = '" . $lang_id . "')";
		
		$sql_filter = "";
		if (!empty($data['filter_display_name'])) {
			$sql_filter .= " AND LCASE(ad.display_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_display_name'])) . "%'";
		}
		if (!empty($data['filter_group_display_name'])) {
			$sql_filter .= " AND LCASE(agd.display_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_group_display_name'])) . "%'";
		}
		if (!empty($data['filter_group_id'])) {
			$sql_filter .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_group_id']) . "'";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
		$sort_data = array(
			'ad.display_name',
			'group_display_name'
		);	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY ad.display_name";	
		}	
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		$query = $this->db->query($sql);
		return $query->rows;
	}
	public function getTotalAttributes($data = array()) {
		$lang_id = (int)$this->config->get('config_language_id');
		$sql = "SELECT COUNT(*) AS total FROM " . 
			DB_PREFIX . "attribute a LEFT JOIN " . 
			DB_PREFIX . "attribute_description ad ON (a.`attribute_id` = ad.`attribute_id` AND ad.language_id = '" . $lang_id . "') LEFT JOIN " . 
			DB_PREFIX . "attribute_group ag ON a.`attribute_group_id` = ag.attribute_group_id LEFT JOIN " . 
			DB_PREFIX . "attribute_group_description agd ON (ag.`attribute_group_id` = agd.attribute_group_id AND agd.language_id = '" . $lang_id . "')";
		
		$sql_filter = "";
		if (!empty($data['filter_display_name'])) {
			$sql_filter .= " AND LCASE(ad.display_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_display_name'])) . "%'";
		}
		if (!empty($data['filter_group_display_name'])) {
			$sql_filter .= " AND LCASE(agd.display_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_group_display_name'])) . "%'";
		}
		if (!empty($data['filter_group_id'])) {
			$sql_filter .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_group_id']) . "'";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
		$query = $this->db->query($sql);
		return $query->row['total'];
	}
}
?>